<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const numSubarrayBoundedMax = function (nums, left, right) {
      const calc = (a, k) => {
        let res = 0
        for (let i = 0, j = 0; i < a.length; i++) {
          if (a[i] > k) j = i + 1
          res += i - j + 1
        }

        return res

      }
      return calc(nums, right) - calc(nums, left - 1)
    };
    console.log(numSubarrayBoundedMax(nums = [2, 9, 2, 5, 6], left = 2, right = 8));
  </script>
</body>

</html>